package rexsee.core.progress;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.lang.RexseeLanguage;
import rexsee.core.widget.ProgressDiv;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class RexseeProgressBarDialog implements JavascriptInterface {

	public static final String INTERFACE_NAME = "ProgressBarDialog";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeProgressBarDialog(childBrowser);
	}

	private AlertDialog mBarProgressDialog = null;

	private final Context mContext;
	private final Browser mBrowser;

	public RexseeProgressBarDialog(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
	}

	//JavaScript Interface
	public void show(final String title, final String message, final String progressInt, final String progressText, final String cancelCallback) {
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				String[] pi = progressInt.split("\\|");
				String[] pt = progressText.split("\\|");
				if (mBarProgressDialog == null) {
					mBarProgressDialog = new AlertDialog.Builder(mContext).create();
					mBarProgressDialog.setCancelable(false);
					mBarProgressDialog.setOnKeyListener(new Dialog.OnKeyListener() {
						@Override
						public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
							if (arg1 == KeyEvent.KEYCODE_SEARCH) return true;
							else return false;
						}
					});
					LinearLayout progressLayout = new LinearLayout(mContext);
					progressLayout.setOrientation(LinearLayout.VERTICAL);
					TextView msg = new TextView(mContext);
					msg.setId(10001);
					msg.setPadding(0, 0, 0, 15);
					msg.setTextSize(18);
					msg.setTextColor(Color.WHITE);
					progressLayout.addView(msg, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
					for (int i = 0; i < pi.length; i++) {
						if (i != 0) {
							progressLayout.addView(new ImageView(mContext), new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 10));
						}
						ProgressDiv bar = new ProgressDiv(mContext);
						bar.setId(i);
						progressLayout.addView(bar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 28));
					}
					ScrollView scroller = new ScrollView(mContext);
					scroller.setVerticalFadingEdgeEnabled(false);
					scroller.setHorizontalFadingEdgeEnabled(false);
					scroller.addView(progressLayout);
					LinearLayout layout = new LinearLayout(mContext);
					layout.setPadding(15, 15, 15, 15);
					layout.addView(scroller, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
					mBarProgressDialog.setView(layout);
				}
				mBarProgressDialog.setIcon(mBrowser.application.icon);
				mBarProgressDialog.setTitle(Html.fromHtml(title));
				if (cancelCallback.compareTo("") != 0) {
					mBarProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, mContext.getResources().getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							mBrowser.loadUrl(cancelCallback);
						}
					});
				}
				mBarProgressDialog.show();
				TextView msg = (TextView) mBarProgressDialog.findViewById(10001);
				msg.setText(Html.fromHtml(message));
				msg.setVisibility((message.compareTo("") != 0) ? View.VISIBLE : View.GONE);
				for (int i = 0; i < pi.length; i++) {
					try {
						((ProgressDiv) mBarProgressDialog.findViewById(i)).setStyle("height:26px;progress:" + Integer.parseInt(pi[i]) + ";label:" + ((pt != null && pt.length > i) ? pt[i] : RexseeLanguage.PROGRESS_WAITING));
					} catch (Exception e) {
					}
				}
			}
		});
	}
	public void show(String progressInt, String progressText) {
		show(RexseeLanguage.TITLE_PROGRESS, "", progressInt, progressText, "");
	}

	public void hide() {
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					if (mBarProgressDialog != null) {
						mBarProgressDialog.dismiss();
						mBarProgressDialog = null;
					}
				} catch (Exception e) {
				}
			}
		});
	}

}
